# -*- coding: UTF-8 -*-
# @Time : 2022/1/17 0017 17:11
# @Author : lyw
# @Remark : 门禁
import logging

from mxsoftpy import View


from server.common.IOTManager.Janitor.door import GetRECDoorServer, SelectDoorServer, GetDoorInfoServer
from utils.classes.custom_exception import VerifyError
from utils.public.params_method import process
from index import _


class GetDoor(View):

    def get(self):
        """ 获取门禁数据 """
        param = self.request.GET
        param['origin_id'] = process(param, 'origin_id', '')  # 数据源查询
        param['search_door'] = process(param, 'search_door', '')  # 模糊搜索
        param['pageindex'] = int(process(param, 'pageindex', 1))  # 默认 为第一页
        param['pagesize'] = int(process(param, 'pagesize', 10))  # 默认10，每页显示数量
        try:
            """res 即 response 返回结果"""
            res = GetDoorInfoServer().get_door_info(**param)
        except TypeError as e:
            logging.error(e)
            raise VerifyError(_("您的参数格式不对，请查看接口文档"))
        return _('查询成功'), res


class Door(View):
    def get(self):
        """
        获取未采集的门禁
        @return:
        """
        return _("查询成功"), GetRECDoorServer().get_rec_door(**self.request.GET)

    def post(self):
        """添加门禁"""
        param = self.request.POST
        if not param.get("door_id") or not param.get("host"):
            return "door_id或者host参数不存在..."
        return _("查询成功"), SelectDoorServer().select_door(**param)
